package com.meixiang.activity.account.managers;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.tool.Tool;
import com.meixiang.util.AbToastUtil;
import com.meixiang.view.CheckEditTextEmptyButton;
import com.meixiang.view.ClearEditText;
import com.meixiang.view.TitleView;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 绑定银行卡界面
 *
 * @author LZRUI
 */
public class BindBankCardActivity extends BaseActivity {
    @Bind(R.id.title)
    TitleView title;
    @Bind(R.id.tv_card_user_name)
    TextView tvCardUserName;//用户名
    @Bind(R.id.tv_bank_name)
    TextView tvBankName;//银行卡的名字
    @Bind(R.id.ll_select_bank_name)
    LinearLayout llSelectBankName;
    @Bind(R.id.et_bank_card_num)
    ClearEditText etBankCardNum;//银行开号
    @Bind(R.id.et_open_bank)
    ClearEditText etOpenBank;
    @Bind(R.id.tv_confirm)
    CheckEditTextEmptyButton tvConfirm;
    private String bankId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_bind_bank_card);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        title.setTitle("绑定银行卡");
        title.showRightButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });
        title.setLeftImageButton(R.mipmap.back);
        title.showLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initView() {

        tvConfirm.setEditText(new TextView[]{tvBankName, etBankCardNum, etOpenBank});
        tvCardUserName.setText(getIntent().getStringExtra("realname"));

    }

    @Override
    protected void setData() {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }


    @OnClick({R.id.ll_select_bank_name, R.id.tv_confirm})
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.ll_select_bank_name:
                if (Tool.isFastDoubleClick()) return;
                Intent intent = new Intent(this, ChooseBankCardActivity.class);
                startActivityForResult(intent, 100);
                break;
            case R.id.tv_confirm:// 确认绑定
//                if (!Tool.checkBankCard(etBankCardNum.getText().toString())){
//                    AbToastUtil.showToast(this, "银行卡号不正确!");
//                    return;
//                }
                if (etOpenBank.getText().toString().equals("")) {
                    AbToastUtil.showToast(this, "开户支行不能为空!");
                    return;
                }
                getBankBind();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 200) {
            tvBankName.setText(data.getStringExtra("bankName"));
            bankId = data.getStringExtra("bankId");
        }
    }

    /**
     * 绑定银行卡
     * depositBank	String	必填			开户行
     * bankCode	String	必填			卡号
     * bankId	String	必填			开户银行ID
     * realName	String	必填			姓名
     */
    private void getBankBind() {
        HttpParams params = new HttpParams();
        params.put("depositBank", etOpenBank.getText().toString());//开户行
        params.put("bankCode", etBankCardNum.getText().toString());//卡号
        params.put("bankId", bankId);//开户银行ID
        params.put("realName", tvCardUserName.getText().toString());//姓名

        HttpUtils.post(Config.MY_ACCOUNT_BANKBIND, params, new HttpCallBack(BindBankCardActivity.this) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                startActivity(new Intent(BindBankCardActivity.this, ManageBankCardActivity.class));
                Toast.makeText(BindBankCardActivity.this, message, Toast.LENGTH_LONG).show();

            }

            @Override
            public void onError(String code, String message) {
                Toast.makeText(BindBankCardActivity.this, message, Toast.LENGTH_LONG).show();
            }
        });
    }
}
